package com.example.phone.home;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.example.phone.base.RequestInterface;
import com.example.phone.base.ServerResponse;
import com.example.phone.bean.Category;
import com.example.phone.bean.MyPageInfo;
import com.example.phone.bean.ParentCategory;
import com.example.phone.bean.Product;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RequestData {

    private Product productDetails = new Product();
    private List<Product> productList = new ArrayList<>();
    private List<Category> categoryList = new ArrayList<>();
    private List<ParentCategory> parentCategoryList = new ArrayList<>();


    public Handler myHandler = new Handler(Looper.getMainLooper());
    public UpdateData mUpdateData;

    //更新数据接口
    public interface UpdateData {
        void onSuccess(Object object);
        void onFail(String error);
    }

    public RequestData(UpdateData updateData) {
        mUpdateData = updateData;
    }

    //发送获取商品信息请求
    public  void sendRequestAllInfo() {
        getProductList();
    }

    //发送获取商品分类请求
    public void sendRequestCategory() {
        getParentCategory();
    }

    //发送获取分类后的商品信息
    public void sendRequestCategoryProduct(int parentId) {
        getCategoryProduct(parentId);
    }

    //发送获取商品详情信息
    public void sendRequestProductDetails(int productId) {
        getProductDetails(productId);
    }

    //获取商品信息
    private void getProductList() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(RequestInterface.basePath)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        //获取全部商品信息
        RequestInterface.getProductsList2 getProductsList2 = retrofit.create(RequestInterface.getProductsList2.class);
        Call<ServerResponse<MyPageInfo<Product>>> call = getProductsList2.getProducts(1, 38, null);

        //发送获取全部商品信息请求
        call.enqueue(new Callback<ServerResponse<MyPageInfo<Product>>>() {

            @Override
            public void onResponse(Call<ServerResponse<MyPageInfo<Product>>> call, Response<ServerResponse<MyPageInfo<Product>>> response) {
                /**
                 * response.body()获取到返回的ServerResponse，再通过获取Status判断是否
                 * 登陆成功/请求数据成功
                 */
                //Log.d("HomeFragment", "登录成功");

                if (response.body() == null) {
                    myHandler.post( new Runnable() {
                        @Override
                        public void run() {
                            mUpdateData.onFail( "未获取相关数据！" );
                        }
                    } );
                    return;
                }
                //log(response.body().getMsg());
                if (response.body().getStatus() == 0) {
                    //获取返回的数据（就是泛型ServerResponse中的T所代表的数据）
                    MyPageInfo myPageInfo =  response.body().getData();
                    Log.d("RequestData", "onResponse: mypageInfo = " + myPageInfo.getList());
                    productList.addAll(myPageInfo.getList());
                    mUpdateData.onSuccess(productList);

                } else {
                    myHandler.post( new Runnable() {
                        @Override
                        public void run() {
                            mUpdateData.onFail( "请求错误！" );
                        }
                    } );
                }
            }

            @Override
            public void onFailure(Call<ServerResponse<MyPageInfo<Product>>> call, Throwable t) {
                //Log.d("HomeFragment", "登录失败 "+t.getMessage());
                myHandler.post( new Runnable() {
                    @Override
                    public void run() {
                        mUpdateData.onFail( "请求数据失败！" );
                    }
                } );
            }
        });
    }

    //获取分类信息
    private void getParentCategory() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(RequestInterface.basePath)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        //获取商品分类信息
        RequestInterface.getParentCategory getParentCategory = retrofit.create(RequestInterface.getParentCategory.class);
        Call<ServerResponse<List<ParentCategory>>> parentCategoryCall = getParentCategory.getParentCategorys( "","");

        //发送获取全部商品信息请求
        parentCategoryCall.enqueue(new Callback<ServerResponse<List<ParentCategory>>>() {

            @Override
            public void onResponse(Call<ServerResponse<List<ParentCategory>>> call, Response<ServerResponse<List<ParentCategory>>> response) {
                /**
                 * response.body()获取到返回的ServerResponse，再通过获取Status判断是否
                 * 登陆成功/请求数据成功
                 */
                //Log.d("HomeFragment", "登录成功");

                if (response.body() == null) {
                    myHandler.post( new Runnable() {
                        @Override
                        public void run() {
                            mUpdateData.onFail( "分类请求：未获取相关数据！" );
                        }
                    } );
                    return;
                }
                //log(response.body().getMsg());
                if (response.body().getStatus() == 0) {
                    //获取返回的数据（就是泛型ServerResponse中的T所代表的数据）
                    List parentCategory = response.body().getData();
                    parentCategoryList.addAll(parentCategory);
                    mUpdateData.onSuccess(parentCategoryList);

                } else {
                    myHandler.post( new Runnable() {
                        @Override
                        public void run() {
                            mUpdateData.onFail( "请求错误！" );
                        }
                    } );
                }
            }

            @Override
            public void onFailure(Call<ServerResponse<List<ParentCategory>>> call, Throwable t) {
                //Log.d("HomeFragment", "登录失败 "+t.getMessage());
                myHandler.post( new Runnable() {
                    @Override
                    public void run() {
                        mUpdateData.onFail( "请求数据失败！" );
                    }
                } );
            }
        });
    }

    //获取分类商品信息
    private void getCategoryProduct(int parentId) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(RequestInterface.basePath)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        //获取全部商品信息
        RequestInterface.getCategorpyProducts getCategorpyProducts = retrofit.create(RequestInterface.getCategorpyProducts.class);
        Call<ServerResponse<List<Category>>> call = getCategorpyProducts.getCategoryProducts(parentId);

        //发送获取全部商品信息请求
        call.enqueue(new Callback<ServerResponse<List<Category>>>() {

            @Override
            public void onResponse(Call<ServerResponse<List<Category>>> call, Response<ServerResponse<List<Category>>> response) {
                /**
                 * response.body()获取到返回的ServerResponse，再通过获取Status判断是否
                 * 登陆成功/请求数据成功
                 */
                //Log.d("HomeFragment", "登录成功");

                if (response.body() == null) {
                    myHandler.post( new Runnable() {
                        @Override
                        public void run() {
                            mUpdateData.onFail( "未获取相关数据！" );
                        }
                    } );
                    return;
                }
                //log(response.body().getMsg());
                if (response.body().getStatus() == 0) {
                    //获取返回的数据（就是泛型ServerResponse中的T所代表的数据）
                    List categoryProduct = response.body().getData();
                    categoryList.addAll(categoryProduct);
                    mUpdateData.onSuccess(categoryList);

                } else {
                    myHandler.post( new Runnable() {
                        @Override
                        public void run() {
                            mUpdateData.onFail( "请求错误！" );
                        }
                    } );
                }
            }

            @Override
            public void onFailure(Call<ServerResponse<List<Category>>> call, Throwable t) {
                //Log.d("HomeFragment", "登录失败 "+t.getMessage());
                myHandler.post( new Runnable() {
                    @Override
                    public void run() {
                        mUpdateData.onFail( "请求数据失败！" );
                    }
                } );
            }
        });
    }

    //获取商品详情信息
    private void getProductDetails(int productId) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(RequestInterface.basePath)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        //获取全部商品信息
        RequestInterface.getProductDetails getProductDetails = retrofit.create(RequestInterface.getProductDetails.class);
        Call<ServerResponse<Product>> call = getProductDetails.getCategoryProducts(productId);

        //发送获取全部商品信息请求
        call.enqueue(new Callback<ServerResponse<Product>>() {

            @Override
            public void onResponse(Call<ServerResponse<Product>> call, Response<ServerResponse<Product>> response) {
                /**
                 * response.body()获取到返回的ServerResponse，再通过获取Status判断是否
                 * 登陆成功/请求数据成功
                 */
                //Log.d("HomeFragment", "登录成功");

                if (response.body() == null) {
                    myHandler.post( new Runnable() {
                        @Override
                        public void run() {
                            mUpdateData.onFail( "未获取相关数据！" );
                        }
                    } );
                    return;
                }
                //log(response.body().getMsg());
                if (response.body().getStatus() == 0) {
                    //获取返回的数据（就是泛型ServerResponse中的T所代表的数据）
                    productDetails = response.body().getData();
                    mUpdateData.onSuccess(productDetails);

                } else {
                    myHandler.post( new Runnable() {
                        @Override
                        public void run() {
                            mUpdateData.onFail( "请求错误！" );
                        }
                    } );
                }
            }

            @Override
            public void onFailure(Call<ServerResponse<Product>> call, Throwable t) {
                //Log.d("HomeFragment", "登录失败 "+t.getMessage());
                myHandler.post( new Runnable() {
                    @Override
                    public void run() {
                        mUpdateData.onFail( "请求数据失败！" );
                    }
                } );
            }
        });
    }

}
